﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace MyVoteMVC.Data
{
    /// <summary>
    /// 将导致数据库重建，原数据丢失
    /// </summary>
    public class VoteDbInitializer : DropCreateDatabaseAlways<VoteDBContext>
    {
        protected override void Seed(VoteDBContext context)
        {
            //for VT_Activities
            context.Database.ExecuteSqlCommand(@"Create Trigger del_activity_001
on dbo.VT_Activities
For Delete
As
If (SELECT COUNT(*) from Deleted) > 0
 Delete From dbo.VT_Activities where Activity_PID in (select Activity_ID from Deleted)");
            context.Database.ExecuteSqlCommand("ALTER TABLE [dbo].[VT_Activities] DROP CONSTRAINT [VT_Activities_ParentActivity]");
            context.Database.ExecuteSqlCommand("ALTER TABLE [dbo].[VT_Items] DROP CONSTRAINT [VT_Items_Project]");
            //for VT_Users
            context.Database.ExecuteSqlCommand("ALTER TABLE dbo.VT_Users add constraint uc_UserName UNIQUE(User_Name)");
            context.Database.ExecuteSqlCommand("ALTER TABLE dbo.VT_Users add constraint uc_UserEmail UNIQUE(User_Email)");
            context.Database.ExecuteSqlCommand("ALTER TABLE dbo.VT_Dictionaries add constraint uc_DictionaryName UNIQUE(Dictionary_Name)");
            //for VT_Tickets
            context.Database.ExecuteSqlCommand("ALTER TABLE dbo.VT_Tickets add constraint uc_TicketNo UNIQUE(Ticket_No)");

            context.Database.ExecuteSqlCommand("INSERT INTO dbo.VT_Dictionaries(Dictionary_ID,Dictionary_Name,Dictionary_Value,Dictionary_Type,Dictionary_Order)values('123456789','邮件主机','mail.gzdi.com','邮件设置',1)");
            context.Database.ExecuteSqlCommand("INSERT INTO dbo.VT_Dictionaries(Dictionary_ID,Dictionary_Name,Dictionary_Value,Dictionary_Type,Dictionary_Order)values('234567981','发送邮件的地址','mailtest@gzdi.com','邮件设置',2)");
            context.Database.ExecuteSqlCommand("INSERT INTO dbo.VT_Dictionaries(Dictionary_ID,Dictionary_Name,Dictionary_Value,Dictionary_Type,Dictionary_Order)values('345689712','发送邮件的密码','mail@gzdi','邮件设置',3)");
            context.Database.ExecuteSqlCommand("INSERT INTO dbo.VT_Dictionaries(Dictionary_ID,Dictionary_Name,Dictionary_Value,Dictionary_Type,Dictionary_Order)values('345689713','帮助文档地址','www.google.com.hk','帮助文档',4)");
      
        
        }
    }

    /// <summary>
    /// 将导致数据库重建，原数据丢失
    /// </summary>
    public class VoteDbInitializerIfModelChanges : DropCreateDatabaseIfModelChanges<VoteDBContext>
    {
        protected override void Seed(VoteDBContext context)
        {
            context.Database.ExecuteSqlCommand(@"Create Trigger del_activity_001
on dbo.VT_Activities
For Delete
As
If (SELECT COUNT(*) from Deleted) > 0
 Delete From dbo.VT_Activities where Activity_PID in (select Activity_ID from Deleted)");

            //for VT_Users
            context.Database.ExecuteSqlCommand("ALTER TABLE dbo.VT_Users add constraint uc_UserName UNIQUE(User_Name)");
            context.Database.ExecuteSqlCommand("ALTER TABLE dbo.VT_Users add constraint uc_UserEmail UNIQUE(User_Email)");
        }
    }
}
